﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2021/12/28/周二
 * 时间: 16:40
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace srt2morph
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private string alertText = "输入SRT字幕脚本 或 拖入SRT文件";
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		void TextBox1Leave(object sender, EventArgs e)
		{
			if (textBox1.Text=="") {
				textBox1.Text=alertText;
				textBox1.ForeColor=ColorTranslator.FromHtml("#FF666666");
				textBox1.Font=new Font(this.Font, FontStyle.Italic );
			}
		}
		void TextBox1Enter(object sender, EventArgs e)
		{
			if (textBox1.Text==alertText) {
				textBox1.Clear();
				textBox1.ForeColor=ColorTranslator.FromHtml("#FF000000");
				textBox1.Font=new Font(this.Font, FontStyle.Regular );
			}
		}
		void TextBox1DragDrop(object sender, DragEventArgs e)
		{
			string srtpath= ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
			textBox1.Text=File.ReadAllText(srtpath);
		}
		void TextBox1DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
		}
		
		public int n=0;
		public string track0,track1,track2,track3;
		public string start,end;
		
		void Button1Click(object sender, EventArgs e)
		{
			string head="[exedit]\nwidth=1280\nheight=720\nrate="+textBox3.Text+"\nscale=1\nlength=0\naudio_rate=44100\naudio_ch=2\n";
			
			string exo="";
			double fps=double.Parse(textBox3.Text);
			for (int i = 0; i < GetSrtLineNum(textBox1.Text); i++){
				exo+=CreatEXO(fps,textBox1.Text,i);
				
			}
			//修复bug********************
			 
			string[] split=exo.Split('\n');
			string[] a=new string[split.Length/22];
			string[] b=new string[split.Length/22];
			for (int i = 0; i < split.Length/22; i++) {
				a[i]=split[i*22+1].Replace("start=","").Trim();
				b[i]=split[i*22+2].Replace("end=","").Trim();
				
			}
			 
			for (int i = 1; i < split.Length/22; i++) {
				if (a[i]!=(int.Parse(b[i-1])+1).ToString()) {
					a[i]=(int.Parse(b[i-1])+1).ToString();
				}
			}
			
			for (int i = 0; i < split.Length/22; i++) {
				split[i*22+1]="start="+a[i]+"\n";
				split[i*22+2]="end="+b[i]+"\n";
			}
			
			split[6]="";
			split[13]+="type=0\nfilter=2\n"+"name="+textBox4.Text.Trim()+"\nparam=\n";
			
			exo=string.Join("",split);
			
			
			SaveFileDialog saveFileDialog = new SaveFileDialog
			{
				Filter = "exo File|*.exo",
				FileName = "表情",
				AddExtension = true
			};
			saveFileDialog.ShowDialog();
			StreamWriter streamWriter = new StreamWriter(saveFileDialog.OpenFile(), Encoding.GetEncoding("GB2312"));
				streamWriter.Write(head+exo);
				streamWriter.Close();
				
			//File.WriteAllText("表情.exo",head+exo,Encoding.GetEncoding("GB2312"));
		}
		
		//		1,41
		//		41,81
		//		81,120  获取每个字的间隔
		public string[] GetTime(string start,string end,int num){
				string[] space=new string[num];
				int k=(int.Parse(end)-int.Parse(start))/num+1;
				string kk="";
				string kkk="";
				for (int i = 0; i < num; i++) {
					kkk=kk;
					kk=(int.Parse(start)+k*(i+1)-1).ToString();
					
					if (i==0) {
						space[i]=start+","+kk;
					}
					else if (i==num-1) {
						space[i]=(int.Parse(start)+k*i).ToString()+","+end;
					}else {
						
						space[i]=int.Parse(kkk)+1+","+kk;
					}
					
				}
				
				return space;
		}
		
		public string MorphToNum(string morphname){
			string num="";
			switch(morphname){
		    case "A":
				num= "2";
		       break; 
		    case "I":
				num= "3";
		       break; 
		    case "U":
				num="4";
		       break; 
		    case "E":
				num="5";
		       break;
			case "O":
				num="6";
				break; 	}
			return num;
		}
		 
		string lastmorph="1";
		public string CreatEXO(double fps,string srt,int i ){
			 
			
			string[] tt=GetContent(textBox1.Text,fps);
			Pinyin py=new Pinyin();
			string morph=py.convertCh(tt[i]);//A,O,E
			string[] kouxing=morph.Split(',');
			
			string[] se=GetStartAndEnd(srt,fps);//100-200  帧范围
				string[] StartAndEnd=se[i].Split('-'); 
				start=StartAndEnd[0];
				end=StartAndEnd[1];
				
				int len=kouxing.Length-1;
				
				 
			
			track0=""; //轨道1
			
			string exo="";
			for (int loop = 0; loop < len; loop++) {
					
					string[] t=GetTime(start,end,len);
					string[] kt=t[loop].Split(',');
					
					track0=lastmorph+","+MorphToNum(kouxing[loop])+",3";
					lastmorph=MorphToNum(kouxing[loop]);
					
					string obj=textBox2.Text;
				
					obj=obj.Replace("{i}",n.ToString());
					
					
					obj=obj.Replace("{start}",kt[0]);
					obj=obj.Replace("{end}",kt[1]);
					
					obj=obj.Replace("{track0}",track0);
					exo+=obj;
					n++;
					 
			}
			 
			 
			return exo;
			  
		}
		public int GetSrtLineNum(string srt){
			MatchCollection matchCollection = Regex.Matches(srt, "(?<sh>[0-9]+):(?<sm>[0-9]+):(?<ss>[0-9]+),(?<sms>[0-9]+).*-->.*(?<eh>[0-9]+):(?<em>[0-9]+):(?<es>[0-9]+),(?<ems>[0-9]+).*\n(?<c>.*)\n");
			return	matchCollection.Count;
		}
		
		public string GetSrt(string srt,double fps){
			MatchCollection matchCollection = Regex.Matches(srt, "(?<sh>[0-9]+):(?<sm>[0-9]+):(?<ss>[0-9]+),(?<sms>[0-9]+).*-->.*(?<eh>[0-9]+):(?<em>[0-9]+):(?<es>[0-9]+),(?<ems>[0-9]+).*\n(?<c>.*)\n");
				double num = fps;
				int num2 = 0;
				string a="",b="",c="",d="";
				foreach (Match item in matchCollection)
				{
					uint num3 = uint.Parse(item.Groups["sh"].Value);
					uint num4 = uint.Parse(item.Groups["sm"].Value);
					uint num5 = uint.Parse(item.Groups["ss"].Value);
					double num6 = uint.Parse(item.Groups["sms"].Value);
					uint num7 = uint.Parse(item.Groups["eh"].Value);
					uint num8 = uint.Parse(item.Groups["em"].Value);
					uint num9 = uint.Parse(item.Groups["es"].Value);
					double num10 = uint.Parse(item.Groups["ems"].Value);
					a+= num3 + ":" + num4 + ":" + num5 + "," + num6 + " - " + num7 + ":" + num8 + ":" + num9 + "," + num10+"\n";
					double num11 = (double)(num3 * 3600 + num4 * 60 + num5) + num6 / 1000.0;
					double num12 = (double)(num7 * 3600 + num8 * 60 + num9) + num10 / 1000.0;
					b+= num11 + " - " + num12+"\n";
					uint num13 = uint.Parse((Math.Floor(num11 * num) + 1.0).ToString());
					uint num14 = uint.Parse(Math.Floor(num12 * num).ToString());
					c+= num13 + " - " + num14+"\n";
					string text2 = item.Groups["c"].Value.TrimEnd('\r', '\n');
					d+= text2 + "\n";
					
					num2++;
				}
				//a 00:01:43,700 --> 00:01:47,050
				//b 秒数范围
				//c 帧数范围
				//d 文字内容
				return c+"@"+d; 
		}
		
		public string[] GetStartAndEnd(string srt,double fps){
			string[] txt=GetSrt(srt,fps).Split('@');
			string[] SE=txt[0].Split('\n');
			return SE;
		}
		
		//获取字幕内容
		public string[] GetContent(string srt,double fps){
			string[] txt=GetSrt(srt,fps).Split('@');
			string[] Content=txt[1].Split('\n');
			return Content;
		}
		
		
		 
		void Button2Click(object sender, EventArgs e)
		{
			string[] space=GetTime("1","120",4);
			string t="";
			for (int i = 0; i < space.Length; i++) {
				t+=space[i];
			}
			MessageBox.Show(t);
		}
		
		
		
		
		
		
		//--------------------------------------
	}
}
